local M = {}
local chezhanGet = {25,50,100,200}
local unit_pos = {}
local blockInfo = {}
--[[
	宝箱：13个月薪水
		公司上市得到100
		牙医费用，支付50
		加油，-50
		朋友归还贷款+75
		本年度银行家+80
		房屋修缮，每个房子付25，每个旅馆付100 意思是lv>1的才算房子，目前没有旅馆 不用管
	四叶草：
		护身符
		免费出班房
		去第一个无主之地,如果没有。就去第一块地
		去最近的车站，如果被买了支付双倍
		去起点
		旅游去夏威夷
		去日本站|前往芬兰（这个估计可以随机）
		后退3格
		前进一步
		奥运会
		龙卷风
		负面卡，给其他玩家任一一个施加一个buff

	wukelan 建设100  抵押100 《 180 150 150 》
	faguo  diya 140   280[280]300

	抵押之后每圈增加抵押费用的10%
]]
local function ShowEvent( str,func,params )
	params = params or {}
	CustomGameEventManager:Send_ServerToAllClients("S2C_SHOW_RANDOM_EVENT",{ str = str,args = params})
	Timers:CreateTimer(function(  )
		func()
	end,2)
end
local sycfunc = {}
sycfunc[1] = function( hero )
	-- 护身符
	ShowEvent("#EVENT_SYC_HSF",function (  )
		addAbilityToHero(hero,"hero_wudi")
	end)
	
	--TODO特效 获取护盾的特效
end
sycfunc[2] = function( hero )
-- 免费出班房
	ShowEvent("#EVENT_SYC_YUEYU",function (  )
		addAbilityToHero(hero,"hero_outwithcard")
	end)

end
sycfunc[3] = function(hero)
-- 去第一个无主之地,如果没有。就去第一块地
	ShowEvent("#EVENT_SYC_WZZD",function (  )
		local id = hero:GetPlayerOwnerID()
		local curPoint = GameRules.positions[id+1]
		local nextidx = roleData.getCityNoOwner(curPoint)
		if nextidx == -1 then
			--没有没被买的地了
			local nextidx = curPoint + 1
			if nextidx > MAX_LEN then
				nextidx = 1
			end
		end
		gameControl[hero:GetUnitName()](hero,nextidx)
	end)
	
end
sycfunc[4] = function(hero)
-- 去最近的车站，如果被买了支付双倍
	ShowEvent("#EVENT_SYC_ZJCZ",function (  )
		local id = hero:GetPlayerOwnerID()
		local curPoint = GameRules.positions[id+1]
		while true do
			curPoint = curPoint + 1
			if curPoint > MAX_LEN then
				curPoint = 1
			end
			local city = roleData.getCityById(curPoint)
			if city.suit == 2 then
				if city.owner and city.owner ~= id then
					hero:AddNewModifier(hero, nil,"modifier_double_fee",{})
				end
				gameControl[hero:GetUnitName()](hero,curPoint)
				return
			end
		end
	end)
	
end
sycfunc[5] = function(hero)
-- 去起点
	ShowEvent("#EVENT_SYC_GOTOSTART",function (  )
		for i,v in ipairs(blockInfo) do
			if v.name == "start" then
				gameControl[hero:GetUnitName()](hero,i)
				return
			end
		end
	end)
	
end
sycfunc[6] = function(hero)
-- 旅游去夏威夷
	ShowEvent("#EVENT_SYC_TOXWY",function (  )
		for i,v in ipairs(blockInfo) do
			if v.name == "xiaweiyi" then
				gameControl[hero:GetUnitName()](hero,i)
				return
			end
		end
	end)
	
end
sycfunc[7] = function(hero)
-- 去日本站|前往芬兰（这个估计可以随机）
	local topoints = {"ribenzhan","fenlan","bolan","xiaweiyi","start"}
	local rd = RandomInt(1,#topoints)
	local name = topoints[rd]
	ShowEvent("#EVENT_SYC_GOTO_RANDOM",function (  )
		for i,v in ipairs(blockInfo) do
			if v.name == name then
				gameControl[hero:GetUnitName()](hero,i)
				return
			end
		end
	end,{name})
	
end
sycfunc[8] = function(hero)
	--后退三格
	ShowEvent("#EVENT_SYC_BACK3",function (  )
		local id = hero:GetPlayerOwnerID()
		local curPoint = GameRules.positions[id+1]
		for i=1,3 do
			curPoint = curPoint - 1
			if curPoint < 1 then
				curPoint = MAX_LEN
			end
		end
		gameControl[hero:GetUnitName()](hero,curPoint,true)
	end)
	
end
sycfunc[9] = function(hero)
	--前进一步
	ShowEvent("#EVENT_SYC_FORWARD1",function (  )
		local id = hero:GetPlayerOwnerID()
		local curPoint = GameRules.positions[id+1]
		curPoint = curPoint + 1
		if curPoint > MAX_LEN then
			curPoint = 1
		end
		gameControl[hero:GetUnitName()](hero,curPoint)
	end)
	
end
sycfunc[10] = function(hero)
	-- 奥运会
	ShowEvent("#EVENT_SYC_AOYUN",function (  )
		addAbilityToHero(hero,"hero_aoyunhui")
	end)
	
end
sycfunc[11] = function(hero)
	-- 龙卷风
	ShowEvent("#EVENT_SYC_TONADO",function (  )
		addAbilityToHero(hero,"hero_tonado")
	end)
	
end
-- sycfunc[12] = function(hero)
-- 	-- 负面卡，给其他玩家任一一个施加一个buff
-- 	--忘记是啥功能了
-- end
local zpstrs = {
	"ZP_GET100",
	"ZP_LOSE100",
	"ZP_DIYA",
	"ZP_SHUHUI",
	"ZP_LVUP2",
	"ZP_LVDOWN2",
	"ZP_MAIDI",
	"ZP_EQUAL1000",
	"ZP_GET500",
	"ZP_ZUOLAO",
}
local zpfunc = {}
zpfunc[1] = function( hero )
	--  +100
	local id = hero:GetPlayerOwnerID()
	ModifyGoldLtx(id,100)
end
zpfunc[2] = function( hero )
	--  -100
	local id = hero:GetPlayerOwnerID()
	ModifyGoldLtx(id,-100)
end
zpfunc[3] = function( hero )
	--diya
	local id = hero:GetPlayerOwnerID()
	local mycities = roleData.getCitiesById(id)
	for _,city in ipairs(mycities) do
		if city.lv == 1 and not city.diya then
			--抵押应该是只能抵押1级的吧？不确定TODOCHECK
			roleData.diyaCityById(id,city.city)
			break
		end
	end
end
zpfunc[4] = function( hero )
	--antidiya
	local id = hero:GetPlayerOwnerID()
	local mycities = roleData.getCitiesById(id)
	for _,city in ipairs(mycities) do
		if city.lv == 1 and city.diya then
			--抵押应该是只能抵押1级的吧？不确定TODOCHECK
			roleData.shCityById(id,city.city)
			break
		end
	end
end
zpfunc[5] = function( hero )
	--lvup * 2
	local id = hero:GetPlayerOwnerID()
	local lved = 0
	while true do
		local citiescanlvup = roleData.getCitiesCanLvup(id)
		for i,cityid in ipairs(citiescanlvup) do
			roleData.lvupCityById(id,cityid)
		end

		lved = lved + 1
		if lved >= 2 then
			break
		end
	end
	
end
--[[
	没盖房子的地方直接盖上房子
	盖房子根据大富翁的规则只能一间间盖上去。不能一个很高，一个没有
]]
zpfunc[6] = function( hero )
	--lvdown * 2
	local id = hero:GetPlayerOwnerID()
	local lved = 0
	while true do
		local citiescanlvup = roleData.getCitiesCanLvdown(id)
		for i,cityid in ipairs(citiescanlvup) do
			roleData.delvCityById(id,cityid)
		end

		lved = lved + 1
		if lved >= 2 then
			break
		end
	end
end
zpfunc[7] = function( hero )
	--maidi
	local id = hero:GetPlayerOwnerID()
	local rd = RandomInt(1,MAX_LEN)
	local start = rd
	while true do
		start = start + 1
		if start > MAX_LEN then
			start = 1
		end
		if start <= rd then
			--没有地可买
			return false
		end
		local city = roleData.getCityById(start)
		if city.owner == nil and city.gold then
			roleData.addCityById(id,start)
			return true
		end
	end
end
zpfunc[8] = function( hero )
	-- =1000
	local id = hero:GetPlayerOwnerID()
	local gold = hero:GetGold()
	local count = gold > 1000 and (gold - 1000) or (1000 - gold)
	ModifyGoldLtx(id,count)
end
zpfunc[9] = function( hero )
	-- +500
	local id = hero:GetPlayerOwnerID()
	ModifyGoldLtx(id,500)
end
zpfunc[10] = function( hero )
	-- ruyu
	M.Zuolao(hero)
end
function M.OnZhuanPan( hero,callback )
	local rd = RandomInt(1,#zpfunc)
	print("OnZhuanPan = ",rd)
	CustomGameEventManager:Send_ServerToAllClients("S2C_TURNTABLE",{dest=rd})
	Timers:CreateTimer(4,function(  )
		zpfunc[rd](hero)
		CustomGameEventManager:Send_ServerToAllClients("S2C_TURNTABLE_CLOSE",{})
		if callback then
			callback(hero:GetPlayerOwnerID())
		end
	end)
end

function M.OnSiyecao( hero,callback )
	local rd = RandomInt(1,#sycfunc)
	print("OnSiyecao = ",rd)
	sycfunc[rd](hero)
	if callback then
		callback(hero:GetPlayerOwnerID())
	end
end

function M.OnCheZhan( hero,callback )
	local id = hero:GetPlayerOwnerID()
	local curPoint = GameRules.positions[id+1]
	local bSuited,count = M.checkSuit(curPoint,id)
	local citydata = roleData.getCityById(curPoint)
	if citydata.owner then
		if citydata.owner == id then
			--有主之地 扣钱在通用操作中做
			--ModifyGoldLtx(citydata.owner,chezhanGet[count],id)
			if roleData.hasChezhanToTravel(id) then
				addAbilityToHero(hero,"hero_travel")
			end
		else
			if hero:HasModifier("modifier_double_fee") then
				local cost = chezhanGet[count] * 2
				if hero:HasModifier("modifier_invulnerable") then
					cost = 0
					hero:RemoveModifierByName("modifier_invulnerable")
				end
				ModifyGoldLtx(city.owner,cost,id)
				hero:RemoveModifierByName("modifier_double_fee")
			end
		end
	end
	if callback then
		callback(id)
	end
end
local function bxEvent( hero,gold,str )
	CustomGameEventManager:Send_ServerToAllClients("S2C_SHOW_RANDOM_EVENT",{ str = str,gold = gold})
	Timers:CreateTimer(function(  )
		local id = hero:GetPlayerOwnerID()
		ModifyGoldLtx(id,gold)
	end,2)
end
local bxfunc = {}
bxfunc[1] = function ( hero )
	-- 13个月薪水
	bxEvent(hero,200,"#EVENT_13MONTH_SALARY")
end
bxfunc[2] = function ( hero )
	-- 公司上市得到100
	bxEvent(hero,100,"#EVENT_GSSS")
end
bxfunc[3] = function ( hero )
	-- 牙医费用，支付50
	bxEvent(hero,-50,"#EVENT_YYFY")
end
bxfunc[4] = function ( hero )
	-- 加油，-50
	bxEvent(hero,-50,"#EVENT_JIAYOU")
end
bxfunc[5] = function ( hero )
	-- 朋友归还贷款+75
	bxEvent(hero,75,"#EVENT_GHDK")
end
bxfunc[6] = function ( hero )
	-- 本年度银行家+80
	bxEvent(hero,80,"#EVENT_BANKIST")
end
bxfunc[7] = function ( hero )
	-- 房屋修缮，每个房子付25，每个旅馆付100 意思是lv>1的才算房子，目前没有旅馆 不用管
	CustomGameEventManager:Send_ServerToAllClients("S2C_SHOW_RANDOM_EVENT",{ str = "#EVENT_FWXS",gold = gold})
	Timers:CreateTimer(function (  )
		local id = hero:GetPlayerOwnerID()
		local mycities = GameRules.pdata[id+1]
		local total = 0
		for i,cityid in ipairs(mycities) do
			local city = roleData.getCityById(cityid)
			if city.lv > 1 then
				total = total + 25
			end
		end
		ModifyGoldLtx(id,-total)
	end,2)
end
bxfunc[8] = function ( hero )
	-- 教育基金到期+100
	bxEvent(hero,100,"#EVENT_JYJJ")
end
function M.OnBaoXiang( hero,callback )
	local rd = RandomInt(1,#bxfunc)
	print("OnBaoXiang = ",rd)
	bxfunc[rd](hero)
	if callback then
		callback(hero:GetPlayerOwnerID())
	end
end

--[[
	block = {
		name,名称
		gold,购买需要花费
		get, 别人踩上去获得的金币
		diya,抵押获得的金钱，赎回一样
		pass,经过这个点就会触发
		func,到达该点触发
		color,颜色
		model,在附近生成模型
		paimai,该房间只能拍卖
		build = {
			升级数据
			{
				gold = 100,升级所需
				get = 100,获得的金币加成

			}
		},
	}
	抵押是一半钱
	卖房子也是一半钱
	收租：= gold / 10 + 
]]

local suitTable = {}
local function calcSuit(  )
	for idx,city in ipairs(blockInfo) do
		if city.suit then
			if not suitTable[city.suit] then
				suitTable[city.suit] = {}
			end
			table.insert(suitTable[city.suit],idx)
		end
	end
end

function M.getCityByName( name )
	for i,v in ipairs(GameRules.blocks) do
		if v.name == name then
			return v
		end
	end
end

function M.getZpstrs(  )
	return zpstrs
end

function M.checkSuit( idx,owner )
	local suitId = blockInfo[idx].suit
	local cities = suitTable[suitId]
	local count = 0
	for _,cityid in ipairs(cities) do
		local city = roleData.getCityById(cityid)
		if city.owner == owner then
			count = count + 1
		end
	end
	if count < #cities then
		return false,count
	end
	return true,count
end

function M.getCitiesBySuit( suitId )
	return suitTable[suitId]
end

function M.getBlockData( idx )
	return blockInfo[idx]
end
function M.getAllBlocks(  )
	return blockInfo
end
function M.getUnitIndexByIdx( idx )
	return GameRules.blocks[idx].unit:entindex()
end
local function doCreateBlock( pos )
	unit = CreateUnitByName("npc_block", pos, true, nil, nil, DOTA_TEAM_NEUTRALS)
	--local gpos = GetGroundPosition(pos,unit)
	--unit:SetAbsOrigin(pos+Vector(0,0,-50))
	unit:SetAbsOrigin(pos+Vector(0,0,-180))
	unit:SetModelScale(3) 
	return unit
end

function M.Zuolao( hero,callback )
	print("trigger prison")
	hero.isInJy = true
	local toPoint = M.getJyIdx()
	-- local block = GameRules.blocks[toPoint]
	-- if not block.cage then
	-- 	block.cage = CreateUnitByName("npc_cage", block.unit:GetAbsOrigin(), true, nil, nil, DOTA_TEAM_NEUTRALS)
	-- end
	gameControl.Zuolao(hero,toPoint)
	if callback then
		callback(hero:GetPlayerOwnerID())
	end
end

function M.getJyIdx(  )
	for i,v in ipairs(blockInfo) do
		if v.name == "jianyu" then
			return i
		end
	end
	return -1
end

local size = 280 --* 0.8
function M.init()
	blockInfo = {
		{
			name = "start",
			color = Vector(255,128,128),
			pass = function(hero)
				--走到这步会触发这个函数
				ModifyGoldLtx(hero:GetPlayerOwnerID(),200)
			end,
			showModel = "models/props_gameplay/crown001.vmdl",
		},
		{
			name = "baoxiang",
			func = M.OnBaoXiang,
			showModel = "models/props_gameplay/treasure_chest001.vmdl",
			scale = 2,
		},
		{
			name = "libiya",
			gold = 50,
			color = Vector(0,0,255),
			suit = 1,
			buildcost = 50,
			build = {
				--TODO 每升一级+的金币
				5,70,150,250,450
			}
		},
		{
			name = "sudan",
			gold = 60,
			suit = 1,
			color = Vector(0,0,255),
			build = {
				--TODO 每升一级+的金币
				30,60,80,420,450
			}
		},
		{
			name = "zhuanpan",
			func = M.OnZhuanPan,
			showModel = "models/props_gameplay/rune_point001.vmdl",
		},
		{
			name = "ribenzhan",
			gold = 200,
			suit = 2,
			func = M.OnCheZhan,
			chezhan = chezhanGet,
			showModel = "models/props_cave/mine_cart.vmdl",
		},
		{
			name = "siyecao",
			func = M.OnSiyecao,
			showModel = "models/props_tree/cornstack/cornstack.vmdl",
		},
		{
			name = "tuerqi",
			gold = 100,
			color = Vector(68,68,68),
			suit = 3,
			build = {
				--TODO 每升一级+的金币
				10,70,150,250,500
			}
		},
		{
			name = "xila",
			gold = 100,
			color = Vector(68,68,68),
			suit = 3,
			build = {
				--TODO 每升一级+的金币
				20,90,180,320,550
			}
		},
		{
			name = "baojialiya",
			gold = 120,
			color = Vector(68,68,68),
			suit = 3,
			build = {
				--TODO 每升一级+的金币
				20,100,200,360,600
			}
		},
		{
			name = "jianyu",
		},
		{
			name = "bolan",
			gold = 150,
			color = Vector(0,255,255),
			suit = 4,
			build = {
				--TODO 每升一级+的金币
				30,120,270,380,550
			}
		},
		{
			name = "eluosi",
			gold = 150,
			color = Vector(0,255,255),
			suit = 4,
			build = {
				--TODO 每升一级+的金币
				30,120,270,380,550
			}
		},
		{
			name = "wukelan",
			gold = 180,
			color = Vector(0,255,255),
			suit = 4,
			build = {
				--TODO 每升一级+的金币
				30,120,270,380,550
			}
		},
		{
			name = "jiaoting",
			suit = 5,
			buildcost = 200,
			build = {
				--TODO 每升一级+的金币
				44,88,555,666,777
			},
			--showModel = "models/props_structures/good_barracks001_destruction.vmdl",
			--paimai = true,
		},
		{
			name = "xizhan",
			gold = 200,
			suit = 2,
			func = M.OnCheZhan,
			chezhan = chezhanGet,
			showModel = "models/props_cave/mine_cart.vmdl",
		},
		{
			name = "baoxiang",
			func = M.OnBaoXiang,
			showModel = "models/props_gameplay/treasure_chest001.vmdl",
			scale = 2,
		},
		{
			name = "litaowan",
			gold = 200,
			suit = 6,
			color = Vector(0,255,0),
			build = {
				--TODO 每升一级+的金币
				70,150,280,380,550
			}
		},
		{
			name = "latuoweiya",
			gold = 200,
			suit = 6,
			color = Vector(0,255,0),
			build = {
				--TODO 每升一级+的金币
				70,190,330,450,660
			}
		},
		{
			name = "aishaniya",
			gold = 220,
			suit = 6,
			color = Vector(0,255,0),
			build = {
				--TODO 每升一级+的金币
				80,210,350,480,760
			}
		},
		{
			name = "tingchechang",
			func = function( hero,callback )
				--如果有停车税就扣钱。TODO


				local id = hero:GetPlayerOwnerID()
				local city = M.getCityByName("tingchechang").unit
				if city.tingcheshui and city.tingcheshui > 0 then
					ModifyGoldLtx(id,city.tingcheshui)
					city.tingcheshui = 0
					if city.tcs_pdx then
						ParticleManager:DestroyParticle(city.tcs_pdx,true)
						city.tcs_pdx = nil
					end
				end
				if callback then
					callback(id)
				end
			end,
			showModel = "models/heroes/rattletrap/rattletrap_cog.vmdl",
		},
		{
			name = "nuowei",
			gold = 220,
			suit = 7,
			color = Vector(255,255,0),
			build = {
				--TODO 每升一级+的金币
				80,210,350,480,760
			}
		},
		{
			name = "ruidian",
			gold = 220,
			suit = 7,
			color = Vector(255,255,0),
			build = {
				--TODO 每升一级+的金币
				80,210,350,480,760
			}
		},
		{
			name = "fenlan",
			color = Vector(255,255,0),
			gold = 240,
			suit = 7,
			build = {
				--TODO 每升一级+的金币
				100,300,750,800,920
			}
		},
		{
			name = "siyecao",
			func = M.OnSiyecao,
			showModel = "models/props_tree/cornstack/cornstack.vmdl",
		},
		{
			name = "meiguozhan",
			gold = 200,
			suit = 2,
			func = M.OnCheZhan,
			chezhan = chezhanGet,
			showModel = "models/props_cave/mine_cart.vmdl",
		},
		{
			name = "zhuanpan",
			func = M.OnZhuanPan,
			showModel = "models/props_gameplay/rune_point001.vmdl",
		},
		{
			name = "deguo",
			gold = 280,
			suit = 8,
			color = Vector(255,0,255),
			build = {
				--TODO 每升一级+的金币
				110,330,550,770,990
			}
		},
		{
			name = "faguo",
			gold = 280,
			suit = 8,
			color = Vector(255,0,255),
			build = {
				--TODO 每升一级+的金币
				110,330,550,770,990
			}
		},
		{
			name = "yingguo",
			gold = 300,
			suit = 8,
			color = Vector(255,0,255),
			build = {
				--TODO 每升一级+的金币
				120,330,550,770,1050
			}
		},
		{
			name = "zuolao",
			func = M.Zuolao,
			showModel = "models/props_structures/dragon_statue001.vmdl",
		},
		{
			name = "jianada",
			gold = 300,
			suit = 9,
			color = Vector(255,128,0),
			build = {
				--TODO 每升一级+的金币
				130,390,550,770,1050
			}
		},
		{
			name = "meiguo",
			gold = 300,
			suit = 9,
			color = Vector(255,128,0),
			build = {
				--TODO 每升一级+的金币
				120,330,550,770,1050
			}
		},
		{
			name = "moxige",
			gold = 320,
			suit = 9,
			color = Vector(255,128,0),
			build = {
				--TODO 每升一级+的金币
				130,350,570,780,1250
			}
		},
		{
			name = "baoxiang",
			func = M.OnBaoXiang,
			showModel = "models/props_gameplay/treasure_chest001.vmdl",
			scale = 2,
		},
		{
			name = "dongzhan",
			gold = 200,
			func = M.OnCheZhan,
			suit = 2,
			chezhan = chezhanGet,
			showModel = "models/props_cave/mine_cart.vmdl",
		},
		{
			name = "siyecao",
			func = M.OnSiyecao,
			showModel = "models/props_tree/cornstack/cornstack.vmdl",
		},
		{
			name = "dibai",
			gold = 360,
			suit = 10,
			color = Vector(255,0,0),
			build = {
				--TODO 每升一级+的金币
				180,350,700,1000,1700
			}
		},
		{
			name = "xiaweiyi",
			gold = 400,
			suit = 10,
			color = Vector(255,0,0),
			build = {
				--TODO 每升一级+的金币
				200,400,800,1300,2000
			}
		},
		{
			name = "tingcheshui",
			func = function ( hero,callback )
				local id = hero:GetPlayerOwnerID()
				ModifyGoldLtx(id,-100)
				local unit = M.getCityByName("tingchechang").unit
				if not unit.tingcheshui then
					unit.tingcheshui = 0
				end
				unit.tingcheshui = unit.tingcheshui + 100
				if unit.tcs_pdx then
					ParticleManager:DestroyParticle(unit.tcs_pdx,true)
					unit.tcs_pdx = nil
				end
				unit.tcs_pdx = PopupGoldNotEnd(unit,unit.tingcheshui)

				if callback then
					callback(id)
				end
			end,
			showModel = "models/items/lone_druid/bear_trap/bear_trap.vmdl",
		},
	}
	--默认初始点为1，其余往上递增
	--开始创建block ， 信息存储到GameRules
	--[[
		1 - 10
		11 - 20 
		21 - 30
		31 - 40
	]]
	calcSuit()
	GameRules.blocks = {}
	
	--起始点：Vector(size / 2 + size * 5,size / 2 + size * 5)
	local startpos = Vector(size / 2 + size * 5,-size / 2 - size * 5,191)
	local bannerpos
	local angel
	for i,info in ipairs(blockInfo) do
		local unit 
		unit_pos[i] = startpos
		local partPos
		if i < 11 then
			unit = doCreateBlock(startpos)
			bannerpos = startpos - Vector(0,size*2 / 3,0)
			partPos = startpos + Vector(0,size / 2,0)
			startpos = Vector(startpos.x - size,startpos.y,startpos.z)
			angel = 90
			--单位放在下面
		elseif i < 21 then
			unit = doCreateBlock(startpos)
			partPos = startpos + Vector(size/2,0,0)
			bannerpos = startpos - Vector(size*2/3,0,0)
			startpos = Vector(startpos.x,startpos.y + size,startpos.z)
			angel = 0
			--单位放在左边
		elseif i < 31 then
			unit = doCreateBlock(startpos)
			partPos = startpos - Vector(0,size /2,0)
			bannerpos = startpos + Vector(0,size *2/ 3,0)
			startpos = Vector(startpos.x + size,startpos.y,startpos.z)
			angel = 90
			--单位放在上面
		else
			unit = doCreateBlock(startpos)
			partPos = startpos - Vector(size /2,0,0)
			bannerpos = startpos + Vector(size *2/ 3,0,0)
			startpos = Vector(startpos.x ,startpos.y - size,startpos.z)
			angel = 0
			--单位放在右边
		end
		if info.color then
			unit:SetRenderColor(info.color.x , info.color.y , info.color.z )
		end
		if info.model then
			unit:SetOriginalModel(info.model)
		end
		local banner
		if info.showModel then
			local showModel = CreateUnitByName("npc_cage_fuck", bannerpos, true, nil, nil, DOTA_TEAM_NEUTRALS)
			local pos = GetGroundPosition(bannerpos,showModel)
			showModel:SetAbsOrigin(pos)
			showModel:SetAngles(0, angel, 0)
			showModel:SetOriginalModel(info.showModel)
			showModel:SetModel(info.showModel)
			if info.scale then
				showModel:SetModelScale(info.scale)
			end
		end
		-- if info.suit then
		-- 	banner = CreateUnitByName("npc_banner", bannerpos, true, nil, nil, DOTA_TEAM_NEUTRALS)
		-- 	bannerpos.z = 10
		-- 	banner:SetAbsOrigin(bannerpos)
		-- 	banner.cityid = i
		-- 	banner:SetAngles(0, angel, 0)
		-- end
		if info.name == "jianyu" then
			local pos = GetGroundPosition(unit:GetAbsOrigin(),unit)
			CreateUnitByName("npc_cage_fuck", pos, true, nil, nil, DOTA_TEAM_NEUTRALS)
		end
		
		unit.cityid = i
		
		local t = {
			["unit"] = unit,
			["gold"] = info.gold,
			["diya"] = info.diya,
			["paimai"] = info.paimai,
			["func"] = info.func,
			["color"] = info.color,
			["build"] = info.build,
			["partPos"] = partPos,
			["banner"] = banner,
			["name"] = info.name,
			["suit"] = info.suit,
			["bannerpos"] = bannerpos,
			["pass"] = info.pass,
			["city"] = i,
			["lv"] = 1,
			["owner"] = nil,
			["chezhan"] = info.chezhan,
			["buildcost"] = info.buildcost,
		}
		table.insert(GameRules.blocks,t)
	end
	print("block inited")
end
local guaijiao = {1,11,21,31}
function M.getGuaijiao(  )
	return guaijiao
end

function M.reCreateParticle(  )
	-- local infos = GameRules.blocks
	-- for i,v in ipairs(infos) do
	-- 	if v.owner then
	-- 		local partID = v.partID
	-- 		local hero = PlayerResource:GetSelectedHeroEntity(v.owner)
	-- 		local pos = GetGroundPosition(v.partPos,hero)
	-- 		local partID = ParticleManager:CreateParticle("particles/items2_fx/owner_tp.vpcf", PATTACH_ABSORIGIN,hero )
	-- 		ParticleManager:SetParticleControlEnt(partID, 3, hero, PATTACH_ABSORIGIN, "follow_origin", pos, true)
	-- 		ParticleManager:SetParticleControl(partID,0,pos)
	-- 		ParticleManager:SetParticleControl(partID,4,Vector(1,0,0))
	-- 		ParticleManager:SetParticleControl(partID, 5, pos)
	-- 		ParticleManager:SetParticleControl(partID,6,Vector(255,255,255))
	-- 	end
	-- end
end

function M.getCityByPosition( pos )
	if pos["0"] then
		pos = Vector(pos["0"],pos["1"],pos["2"])
	end
	for i,_pos in ipairs(unit_pos) do
		local distance = (_pos - pos):Length2D()
		if distance < size / 2 then
			return i
		end
	end
end
--[[
	玩家行动的时候解除石化状态
	particles/status_fx/status_effect_medusa_stone_gaze.vpcf
]]
M.init()

return M

